﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Data.OracleClient;
using System.Text;
using ReflectionStudio.Core.Events;
namespace WPFExcelReport
{
    public class WS03OUTPUTData : BaseFunction
    {
        public bool flagRegenerateLastMonthData;
        ArrayList ListTris = new ArrayList();

        List<WGSummary> ListWGSummary;
        string TrisInclude = "";
        DataTable tbAB50OUTPUT;
        public int ThreadCount;
        public WS03OUTPUTData()
            : base()
        {
            tbAB50OUTPUT = new DataTable("AB50OUTPUT");
            tbAB50OUTPUT.Columns.Add("SUMYEAR", typeof(string));
            tbAB50OUTPUT.Columns.Add("SUMMONTH", typeof(string));
            tbAB50OUTPUT.Columns.Add("LSLIEFNR", typeof(string));
            tbAB50OUTPUT.Columns.Add("FACTORY", typeof(string));
            tbAB50OUTPUT.Columns.Add("BATCHES", typeof(int));
            tbAB50OUTPUT.Columns.Add("SUBQTY", typeof(double));
            tbAB50OUTPUT.Columns.Add("SUBYIELDQTY", typeof(double));
            tbAB50OUTPUT.Columns.Add("G10KGORDERS", typeof(int));
            tbAB50OUTPUT.Columns.Add("OTDORDERS", typeof(int));
            tbAB50OUTPUT.Columns.Add("GOTDORDERS", typeof(int));
            tbAB50OUTPUT.Columns.Add("MAXDELAYDAYS", typeof(int));
            tbAB50OUTPUT.Columns.Add("LOTDORDERS", typeof(int));
            tbAB50OUTPUT.Columns.Add("MAXLESSDAYS", typeof(int));
        }
        public WS03OUTPUTData(Dictionary<string, object> DicGlobalPara)
            : base(DicGlobalPara)
        {
        }

        public DataSet GetRawData(DataRow dwABPOS)
        {
            DataSet set4Return = new DataSet();
            DataSet setLocal = new DataSet();
            DataSet setResult = new DataSet();
            DataSet setTemp = new DataSet();
            DataTable tableLocal = null;
            string VKPAUFNUM;
            string WBSRTDATE, WBENTDATE;
            WBSRTDATE = dwABPOS["WBSRTDATE"].ToString().Trim();
            WBENTDATE = dwABPOS["WBENTDATE"].ToString().Trim();
            /*SELECT DISTINCT LSNAME1 AS FPRODUCER,BMBEM,SUBSTR(P.ABPART,1,1) AS ABPARTTYPE"
                    +",TRIM(P.ABPART)||'-'||TRIM(P.ABPFARBE)||'-'||TRIM(P.ABPVAR) AS ARTICLE"
                    +",ROUND(0.1*SUM(BMSOLLLG),0) AS BMSOLLLG,ROUND(0.01*SUM(P.ABPEALFM),1) AS ABPEALFM,"
                    + "ROUND(100*(ROUND(0.1*SUM(BMSOLLLG),0)-ROUND(0.01*SUM(P.ABPEALFM),1))/ROUND(0.1*SUM(BMSOLLLG),0),2) AS SHRINKAGE"
                    +" FROM  ABPOS P LEFT JOIN LIEFDAT L ON  P.ABPLIEFNR=L.LSLIEFNR"
                    +" LEFT JOIN BABEW W ON BBAUFKRS= P.ABPAUFKRS AND BBAUFLFD=P.ABPAUFLFD AND BBAUFPOS=P.ABPAUFPOS"
                    +" LEFT JOIN BABST ON BMBAUMKRS=BBBAUMKRS AND BMBAUMLFD=BBBAUMLFD"
                    + " WHERE P.ABPAUFART='PR' AND ABPERLKZ='J'"
                    +" AND BMBAUMKRS=BBBAUMKRS AND BMBAUMLFD=BBBAUMLFD"
                    + " AND ABPERLDAT>='" + WBSRTDATE + "' AND ABPERLDAT<='" + WBENTDATE + "' "
                    +" GROUP BY LSNAME1,SUBSTR(P.ABPART,1,1),TRIM(P.ABPART)||'-'||TRIM(P.ABPFARBE)||'-'||TRIM(P.ABPVAR),BMBEM"
                    +" ORDER BY 1,2,3,4"; 
              */
            try
            {
                OracleCommand or1 = OracleDB.CreateCommand();
                #region
                or1.CommandText = "SELECT TRIM(E.LSNAME1) AS FPRODUCER,TRIM(D.BMBEM) AS BMBEM, "
                    + " SUBSTR(A.ABPART, 2, 1) AS ABPARTTYPE, "
                    + " TRIM(A.ABPART) || '-' || TRIM(A.ABPFARBE) || '-' || A.ABPVAR AS ARTICLE, "
                    + " ROUND(SUM(A.ABPZUGET / 100), 0) AS BMSOLLLG,ROUND(SUM(A.ABPEALFM / 100), 1) AS ABPEALFM, "
                    + " ROUND(100 * (ROUND(SUM(A.ABPZUGET / 100), 0) - "
                    + " ROUND(SUM(A.ABPEALFM / 100), 1)) /decode(ROUND(SUM(A.ABPZUGET / 100), 0),0,1,ROUND(SUM(A.ABPZUGET / 100), 0)),2) AS SHRINKAGE,  "
                    + " H.wakeafaktr/100 AS STD "
                    + " FROM ABPOS A, ABFOLGE B, BAZU C, BABST D, LIEFDAT E,ABAUFM F,ABPOS G,wastke H "
                    + " WHERE A.ABPMANDANT = B.ABFMANDANT AND A.ABPAUFKRS = B.ABFAUFKRS "
                    + " AND A.ABPAUFLFD = B.ABFAUFLFD AND A.ABPAUFPOS = B.ABFAUFPOS AND A.ABPMANDANT = C.BZMANDANT "
                    + " AND A.ABPAUFKRS = C.BZAUFKRS AND A.ABPAUFLFD = C.BZAUFLFD AND A.ABPAUFPOS = C.BZAUFPOS "
                    + " AND C.BZMANDANT = D.BMMANDANT AND C.BZBAUMKRS = D.BMBAUMKRS AND C.BZBAUMLFD = D.BMBAUMLFD "
                    + " AND B.ABFMANDANT = E.LSMANDANT AND B.ABFLIEFNR = E.LSLIEFNR AND A.ABPMANDANT = F.ABAMANDANT "
                    + " AND A.ABPAUFKRS = F.ABAAUFKRS AND A.ABPAUFLFD = F.ABAAUFLFD AND A.ABPAUFPOS = F.ABAAUFPOS "
                    + " AND F.ABAMANDANT = G.ABPMANDANT AND F.ABADIAUFKRS = G.ABPAUFKRS AND F.ABADIAUFLFD = G.ABPAUFLFD "
                    + " AND F.ABADIAUFPOS = G.ABPAUFPOS AND F.ABAAPOS = '0001' AND B.ABFMANUELKZ = 'N' "
                    + " AND A.ABPAUFART = 'PR' AND G.ABPERLKZ = 'J' "
                    + " and a.abpmandant = H.wakmandant and a.abpart = H.wakart "
                    + " and trim(a.abpvar) = trim(H.wakvar) and a.abpfarbe = H.wakwcol "
                    + " AND G.ABPERLDAT>='" + WBSRTDATE + "' AND G.ABPERLDAT<='" + WBENTDATE + "' "
                    + " GROUP BY E.LSNAME1, D.BMBEM, A.ABPART, A.ABPVAR, A.ABPFARBE,H.wakeafaktr "
                    + " ORDER BY 1, 2, 3, 4 ";

                   
                #endregion
                setLocal = Connect2Proxy(or1);
                if (setLocal.Tables.Count > 0)
                {
                    tableLocal = setLocal.Tables[0];
                    tableLocal.TableName = "WS03OUTPUT";
                    SetDefault(tableLocal);
                    tbAB50OUTPUT.Clear();
                    byte[] b;
                    string value;
                    string sTemp = "";
                    string Column = "";
                    string ARTICLE = "", WBBDPART, FACTORY;
                    int PCNUM;
                    double WBEWMTR;
                    DataRow dwOUTPUT;
                    List<DataRow> dltRows = new List<DataRow>();
                    ListWGSummary = new List<WGSummary>();
                    string SUMMONTH, SUMYEAR, LSLIEFNR;
                    SUMYEAR = WBSRTDATE.Substring(0, 4);
                    SUMMONTH = WBSRTDATE.Substring(4, 2);
                    int G10KGORDERS;
                    int dYear, dMonth, dDay, eYear, eMonth, eDay;
                    string ABPLIEFDAT, ABPERLDAT;
                    DateTime dDate, eDate;
                    bool hasdDate = false, haseDate = false;
                    bool flagnew = false;
                    string VKPARTTYPE, VKPPOSTYP;
                    foreach (DataRow dwTemp in tableLocal.Rows)
                    {
                        Column = "FPRODUCER";
                        #region
                        b = Encoding.GetEncoding("ISO-8859-1").GetBytes(dwTemp[Column].ToString().Trim());
                        value = Encoding.GetEncoding("gb2312").GetString(b);
                        dwTemp[Column] = value;
                        Column = "ARTICLE";
                        #endregion
                        Column = "ARTICLE";
                        #region
                        b = Encoding.GetEncoding("ISO-8859-1").GetBytes(dwTemp[Column].ToString().Trim());
                        value = Encoding.GetEncoding("gb2312").GetString(b);
                        dwTemp[Column] = value;
                        #endregion
                        Column = "BMBEM";
                        #region
                        b = Encoding.GetEncoding("ISO-8859-1").GetBytes(dwTemp[Column].ToString().Trim());
                        value = Encoding.GetEncoding("gb2312").GetString(b);
                        dwTemp[Column] = value;
                        #endregion
                        VKPARTTYPE = dwTemp["ABPARTTYPE"].ToString();
                        switch (VKPARTTYPE)
                        {
                            case "1":
                                dwTemp["ABPARTTYPE"] = "市场";
                                break;
                            case "3":
                                dwTemp["ABPARTTYPE"] = "高档";
                                break;
                            case "2":
                                dwTemp["ABPARTTYPE"] = "色织";
                                break;
                            case "4":
                                dwTemp["ABPARTTYPE"] = "色织";
                                break;
                            default:
                                dwTemp["ABPARTTYPE"] = "未知";
                                break;
                        } 
                    }
                     
                    setLocal.AcceptChanges();
                    set4Return.Merge(setLocal); 
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return set4Return;
        }

        public DataSet BuildWBBST(DataRow dwUI)
        {
            string progress = "查询完成";
            DataSet setReturn = new DataSet();
            DataSet setResult = new DataSet();
            try
            {
                EventDispatcher.Instance.RaiseStatus("查询开始", StatusEventType.StartProgress);
                setResult = GetRawData(dwUI);
                setReturn.Merge(setResult);
                EventDispatcher.Instance.RaiseStatus("查询完成", StatusEventType.StopProgress);
            }
            catch (Exception ex)
            {
                Tracer.Error(ABPOS + ex.StackTrace, ex);
            }
            return setReturn;
        }
    }
}